package com.qunar.crawler.html;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.Bullet;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qunar.crawler.DB.JdbcUtils;
import com.qunar.crawler.bean.Go117;

public class HtmlPossesser117Go {

	private static Pattern compile = Pattern
			.compile("<meta.*?charset=(\")?(.*?)\".*?>");
	private static Pattern date = Pattern.compile("\\d{4}/\\d{2}/\\d{2}");
	private static Pattern imgUrl = Pattern
			.compile("http://media.breadtrip.com/photos/\\d{4}/\\d{2}/\\d{2}/\\w+.[jJ][pP][Gg]");

	private static Pattern tourListUrl = Pattern
			.compile("http://117go.com/all_tour_list_p/\\d+.html");
	private static Pattern tourUrl = Pattern.compile("tour\\d+.html");

	private static String baseUri = "http://117go.com/";
	
	private static SimpleDateFormat format = new SimpleDateFormat("yyyy");

	private static final Logger logger = LoggerFactory
			.getLogger(HtmlPossesser117Go.class);

	/** --------返回插入的图片数----------- */
	public static void fillUrlQueue(Queue<String> urlQueue) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			long begin = System.currentTimeMillis();
			conn = JdbcUtils.getConnection();
			String sql = "SELECT url FROM go117_tours";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				urlQueue.offer(rs.getString("url"));
			}

			long end = System.currentTimeMillis();
			logger.info("取出117GO旅行URL到内存队列耗时:" + (end - begin) + "ms.");
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			JdbcUtils.release(conn, pstmt, rs);
		}

	}

	/** ---------从url中截取出日期-------------- */
	private static String getDate(String url) {
		Matcher m = date.matcher(url);
		while (m.find()) {
			return m.group();
		}
		return "";
	}

	// dig the date of pic public
	public static String digDate(String html) {

		try {
			NodeFilter filter = new NodeClassFilter(ImageTag.class);

			Parser parser = new Parser();
			parser.setInputHTML(html);
			parser.setEncoding("utf-8");

			NodeList list = parser.extractAllNodesThatMatch(filter);

			if (list.size() > 0) {
				ImageTag img = (ImageTag) list.elementAt(0);

				String url = null;
				url = rebuildDate(img.getImageURL());
				return url;
			}

		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;

	}

	// 121119 --->> 2012/11/19
	private static String rebuildDate(String imageURL) {
		
		Calendar c = Calendar.getInstance();
		
		String year = format.format(c.getTime());
		
		String yearOfsuffix2 = StringUtils.substring(
				year, 0, 2);
//		System.out.println(year);
//		System.out.println(yearOfsuffix2);
		String dateOfUrl = StringUtils.substringBetween(imageURL, "&p=", "/");
		String date;
		String newDate;
		if (dateOfUrl.length() == 6) {
			date = yearOfsuffix2 + dateOfUrl;
		} else {
			date = dateOfUrl;
		}

		newDate = StringUtils.substring(date, 0, 4) + "/"
				+ StringUtils.substring(date, 4, 6) + "/"
				+ StringUtils.substring(date, 6, 8);

		return newDate;
	}
	
//	public static void main(String [] args){
//		System.out.println(rebuildDate("http://img.117go.com/demo27/img/thumb.php?s=300&p=20121118/16f35e325e.jpg"));
//	}

	// 挖掘117bean
	public static void digGo117(String html, List<Go117> go117s) {

		String username = new String();
		try {
			NodeFilter filter = new NodeClassFilter(Bullet.class);

			Parser parser = new Parser();
			parser.setInputHTML(html);
			parser.setEncoding("utf-8");

			NodeList list = parser.extractAllNodesThatMatch(filter);

			for (int i = 0; i < list.size(); i++) {

				Go117 go117 = new Go117();

				Bullet li = (Bullet) list.elementAt(i);
				// 提取旅程图片
				if ("imgbox".trim().equalsIgnoreCase(
						li.getAttribute("class"))) {
					// System.out.println(div.getChild(1).toPlainTextString());
					String url = digImgUrl(li.toHtml());
					go117.setImgUrl(StringUtils.isBlank(url) ? null : url);

					// 提取旅程用户名
					username = digUser(li.toHtml());

					go117.setUsername(username);

					go117.setDate(digDate(li.toHtml()));
					
					go117s.add(go117);
				}

			}

		} catch (ParserException e) {
			e.printStackTrace();
		}
	}

	// dig the name of the tour
	private static String digTitle(String html) {
		// TODO Auto-generated method stub
		return null;
	}

	// dig the username of the tour
	private static String digUser(String html) {

		try {
			NodeFilter filter = new NodeClassFilter(LinkTag.class);

			Parser parser = new Parser();
			parser.setInputHTML(html);
			parser.setEncoding("utf-8");

			NodeList list = parser.extractAllNodesThatMatch(filter);

			if (list.size() > 0) {
				LinkTag link = (LinkTag) list.elementAt(list.size() - 1);

				String username = null;
				username = link.getLinkText();
				return username;
			}

		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 挖掘图片url
	private static String digImgUrl(String html) {

		try {
			NodeFilter filter = new NodeClassFilter(ImageTag.class);

			Parser parser = new Parser();
			parser.setInputHTML(html);
			parser.setEncoding("utf-8");

			NodeList list = parser.extractAllNodesThatMatch(filter);

			if (list.size() > 0) {
				ImageTag img = (ImageTag) list.elementAt(0);

				String url = null;
				url = rebuild(img.getImageURL());
				return url;
			}

		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;
	}

	// rebuild the url 2 the right form
	private static String rebuild(String imageURL) {

		// http://img.117go.com/demo27/img/thumb.php?s=300&p=121118/16f35e325e.jpg
		// ===>>>
		// http://img.117go.com/demo27/img/w300/121118/16f35e325e.jpg

		String first = StringUtils.substringBetween(imageURL, "s=", "&p");
		String second = StringUtils.substringAfter(imageURL, "&p=");
		return "http://img.117go.com/demo27/img/w" + first + "/" + second;
	}

	
	 // 查找所有class=feed-content float-left的div,并从中找出所有用户名称和发的图片URL
    public static void digTourUrls(String html, List<String> tourUrls) {

        try {
            NodeFilter filter = new NodeClassFilter(Div.class);

            Parser parser = new Parser();
            parser.setInputHTML(html);
            parser.setEncoding("utf-8");

            NodeList list = parser.extractAllNodesThatMatch(filter);

            for (int i = 0; i < list.size(); i++) {

                Div node = (Div) list.elementAt(i);
                if ("mbc_Article".equalsIgnoreCase(node.getAttribute("class"))) {
                    // System.out.println(node.toHtml());
                    String url = new String();
                    url = digLink(node.toHtml());
                    tourUrls.add(url.trim());
                }
            }

        } catch (ParserException e) {
            e.printStackTrace();
        }

    }
	
	
	/**
	 * 用正則提取信息 从background-image:url(
	 * 'http://media.breadtrip.com/photos/2012/08/07/2239475fbce965d22f5da3d6f844b1a1.jp
	 * g ' ) 中提取出 http://media.breadtrip.com/photos/2012/08/07/2239475f
	 * bce965d22f5da3d6f844b1a1.jpg
	 * 
	 * @param str
	 * @return String
	 */
	private static String digUrl(Pattern p, String html) {

		Matcher m = p.matcher(html);
		if (m.find())
			return m.group();
		return null;
	}

	private static String digLink(String html) {
		try {
			NodeFilter filter = new NodeClassFilter(LinkTag.class);

			Parser parser = new Parser();
			parser.setInputHTML(html);
			parser.setEncoding("utf-8");

			NodeList list = parser.extractAllNodesThatMatch(filter);
			String url = new String();
			if (list.size() > 0) {
				LinkTag link = (LinkTag) list.elementAt(0);
				url = link.getLink().toLowerCase().trim();
				url = getAbsoluteURL(baseUri, url);
				// System.out.println(url);
			}
			return url;

		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("finally")
	public static String getAbsoluteURL(String baseURI, String relativePath) {
		String abURL = null;
		try {
			URI base = new URI(baseURI);// 基本网页URI
			URI abs = base.resolve(relativePath);// 解析于上述网页的相对URL，得到绝对URI
			URL absURL = abs.toURL();// 转成URL
			abURL = absURL.toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} finally {
			return abURL;
		}
	}
}
